﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 晨曦超市管理系统.进货
{
    public partial class Replenish : Form
    {
        public Replenish()
        {
            InitializeComponent();
        }
        SqlDataReader Duicuo;
        DataTable JIngban;
        private void button1_Click(object sender, EventArgs e)
        {
            textBox14.Text = textBox1.Text.Trim();
            try
            {
                string cha = textBox1.Text;
                string sqlstr = "select * from ProfferBill where SupplierID='" + cha + "'";
                Duicuo = DBHelper.DateReader(sqlstr);
                while (Duicuo.Read())
                {
                    textBox2.Text = Duicuo[1].ToString();
                    textBox3.Text = Duicuo[2].ToString();
                    textBox4.Text = Duicuo[3].ToString();
                    textBox5.Text = Duicuo[4].ToString();
                    textBox6.Text = Duicuo[5].ToString();
                    textBox7.Text = Duicuo[6].ToString();
                    textBox8.Text = Duicuo[7].ToString();
                }
                Duicuo.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("供货商号不能为空");
                    textBox1.Focus();
                    return;
                }
                if (textBox2.Text == "")
                {
                    MessageBox.Show("供货名称不能为空");
                    textBox2.Focus();
                    return;
                }
                if (textBox3.Text == "")
                {
                    MessageBox.Show("联系人不能为空");
                    textBox3.Focus();
                    return;
                }
                if (textBox4.Text == "")
                {
                    MessageBox.Show("联系电话不能为空");
                    textBox4.Focus();
                    return;
                }
                if (textBox5.Text == "")
                {
                    MessageBox.Show("开户银行不能为空");
                    textBox5.Focus();
                    return;
                }
                if (textBox6.Text == "")
                {
                    MessageBox.Show("银行卡号不能为空");
                    textBox6.Focus();
                    return;
                }
                if (textBox7.Text == "")
                {
                    MessageBox.Show("联系地址不能为空");
                    textBox7.Focus();
                    return;
                }
                string SupplierID = textBox1.Text.Trim();
                string Name = textBox2.Text.Trim();
                string Linkman = textBox3.Text.Trim();
                string Phone = textBox4.Text.Trim();
                string ContactAddress = textBox5.Text.Trim();
                string CardDoorBank = textBox6.Text.Trim();
                string BankAccount = textBox7.Text.Trim();
                string Remark = textBox8.Text.Trim();
                string sqlstr = string.Format(@"insert into ProfferBill(SupplierID,Name,Linkman,Phone,ContactAddress,CardDoorBank,BankAccount,Remark)
                                         values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", SupplierID, Name, Linkman, Phone, ContactAddress, CardDoorBank, BankAccount, Remark);
                bool Zen = DBHelper.QEX(sqlstr);
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录异常请尽快联系管理员原因" + ex.Message);
            }
        }
        private void Replenish_Load(object sender, EventArgs e)
        {
            comboBox2.Items.Add("瓶");
            comboBox2.Items.Add("箱");
            comboBox2.Items.Add("包");
            comboBox2.Items.Add("瓶");
            string name = "Name";
            string UserNameID = "UserNameID";
            string name1 = "TypeName";
            string UserNameID1 = "TypeID";
            try
            {
                string sqlstr = "select Name,UserNameID from informationOfEmployees";
                Reusing.XiaLa(comboBox1, name, UserNameID, JIngban, sqlstr);


                string strSql = "SELECT *FROM Type ORDER BY TypeID";
                Reusing.XiaLa(comboBox3, name1, UserNameID1, JIngban, strSql);
            }
            catch (Exception ex)
            {
                //捕获到了异常则提示
                MessageBox.Show(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                //类型
                string TypeName = textBox10.Text.Trim();
                string ParentID = comboBox3.SelectedValue.ToString();
                string sqlstr = "insert into Type(TypeName,ParentID) values('" + TypeName + "'," + ParentID + ")";
                bool duicuo = DBHelper.QEX(sqlstr);


                //进货主表
                string JinHuohao = DateTime.Now.ToString("yyyyMMddHHMMss");
                string ShiJian = dateTimePicker1.Value.ToShortDateString();
                string name = comboBox1.SelectedValue.ToString();
                string SupplierID = textBox1.Text.Trim();
                string sqlstr1 = "insert into ImportBill(BillID,PurchaseDate,UserNameID,SupplierID) values('" + JinHuohao + "','" + ShiJian + "'," + name + ",'" + SupplierID + "')";
                bool duicuo1 = DBHelper.QEX(sqlstr1);
            //进货明细表
                string ProductID = textBox9.Text.Trim();
                string Shuliang = textBox16.Text.Trim();
                double JingHuoJi = Convert.ToDouble(textBox17.Text.Trim());
                string sqlstr2 = "insert into ImportBillDetail(BillID,ProductID,Quantity,JinHuoJi) values('" + JinHuohao + "','" + ProductID + "','" + Shuliang + "','" + JingHuoJi + "')";
                bool duicuo2 = DBHelper.QEX(sqlstr2);
                
            //仓库
                string Name = textBox10.Text.Trim();
                string ShapeCode = textBox11.Text.Trim();
                string TypeID = ParentID;
                string PlaceOfOrigin = textBox13.Text.Trim();
                double UnitPrice = Convert.ToDouble(textBox15.Text.Trim());
                string DateOfManufacture = dateTimePicker2.Value.ToShortDateString();
               // string Shuliang = textBox16.Text.Trim();
               //double JingHuoJi = Convert.ToDouble(textBox17.Text.Trim());
                string UnitOfMeasurement = comboBox2.Text;
                //string SupplierID = textBox1.Text.Trim();
                double Discount = Convert.ToDouble(textBox18.Text);
                
                string Remark = textBox19.Text.Trim();
                if (Discount<0||Discount>1)
                {
                    MessageBox.Show("折扣小于1或大于0");
                    textBox18.Focus();
                    return;
                 }
                string sqlstr3 = "insert into CommodityList(ProductID,SupplierID,Name1,ShapeCode,UnitOfMeasurement,TypeID,PlaceOfOrigin,UnitPrice,DateOfManufacture,Shuliang,JingHuoJi,Discount,Remark)values('" + ProductID + "','" + SupplierID + "','" + Name + "','" + ShapeCode + "','" + UnitOfMeasurement + "'," + TypeID + ",'" + PlaceOfOrigin + "'," + UnitPrice + ",'" + DateOfManufacture + "'," + Shuliang + "," + JingHuoJi + "," + Discount + ",'" + Remark + "')";
                bool duicuo3 = DBHelper.QEX(sqlstr3);
                if (duicuo)
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string ChaXun = textBox9.Text.Trim();
                string sqlstr = "select *  from CommodityList  where ProductID = '" + ChaXun + "'";
                SqlDataReader dr = DBHelper.DateReader(sqlstr);
                while (dr.Read())
                {
                    textBox9.Text = dr[0].ToString();
                    textBox14.Text = dr[1].ToString();
                    textBox10.Text = dr[2].ToString();
                    textBox11.Text = dr[3].ToString();
                    comboBox2.Text = dr[4].ToString();
                    comboBox3.Text = dr[5].ToString();
                    textBox13.Text = dr[6].ToString();
                    textBox17.Text = dr[7].ToString();
                    textBox19.Text = dr[8].ToString();
                    dateTimePicker2.Text = dr[9].ToString();
                    textBox16.Text = dr[10].ToString();
                    textBox15.Text = dr[11].ToString();
                    textBox18.Text = dr[12].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        private void textBox18_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}